package cn.lhn.ridersclub.service;

import cn.lhn.ridersclub.dto.LoginResult;
import cn.lhn.ridersclub.entity.Login;
import cn.lhn.ridersclub.mapper.LoginMapper;
import cn.lhn.ridersclub.util.EncryptUtil;
import cn.lhn.ridersclub.util.JwtUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {

    private final LoginMapper loginMapper;

    private final CaptchaService captchaService;

    public LoginServiceImpl(LoginMapper loginMapper, CaptchaService captchaService) {
        this.loginMapper = loginMapper;
        this.captchaService = captchaService;
    }

    @Override
    public void registerLogin(long id, String email, String password) {
        //  密码加密
        String salt = EncryptUtil.MD5.getSalt(8);
        Login login = new Login(id, email, EncryptUtil.MD5.encrypt(password, salt), salt);

        //  添加登录信息
        int i = loginMapper.insert(login);
        if (i != 1) {
            throw new RuntimeException("注册失败");
        }
    }

    @Override
    public void closeLogin(long id) {
        //  移除登录信息
        int i = loginMapper.deleteById(id);
        if (i != 1) {
            throw new RuntimeException("注销失败");
        }
    }

    @Override
    public LoginResult loginByPassword(String email, String password) {

        //  查询登录信息
        Login login = getLoginByEmail(email);

        //  判断账号是否存在
        if (login == null) {
            throw new RuntimeException("账号不存在");
        }

        //  判断密码是否正确
        String encrypt = EncryptUtil.MD5.encrypt(password, login.getSalt());
        if (!login.getPassword().equals(encrypt)) {
            throw new RuntimeException("密码不正确");
        }

        //  生成Token
        String token =  JwtUtil.createToken(String.valueOf(login.getId()), email);
        return new LoginResult(login.getId(), email, token);
    }

    @Override
    public LoginResult loginByCaptcha(String email, String code) {

        //  查询登录信息
        Login login = getLoginByEmail(email);

        //  判断账号是否存在
        if (login == null) {
            throw new RuntimeException("账号不存在");
        }

        //  验证码校验
        captchaService.verifyCaptcha(email, code);

        //  生成Token
        String token = JwtUtil.createToken(String.valueOf(login.getId()), email);
        return new LoginResult(login.getId(), email, token);
    }

    @Override
    public void updatePassword(String email, String code, String password) {

        //  查询登录信息
        Login login = getLoginByEmail(email);

        //  判断账号是否存在
        if (login == null) {
            throw new RuntimeException("账号不存在");
        }

        //  验证码校验
        captchaService.verifyCaptcha(email, code);

        //  密码加密
        String salt = EncryptUtil.MD5.getSalt(8);
        String encrypt = EncryptUtil.MD5.encrypt(password, salt);

        //  设置新的数据
        login.setPassword(encrypt);
        login.setSalt(salt);

        //  更新记录
        int i = loginMapper.updateById(login);
        if (i != 1) {
            throw new RuntimeException("更新失败");
        }
    }

    @Override
    public Login getLoginByEmail(String email) {
        QueryWrapper<Login> wrapper = new QueryWrapper<>();
        wrapper.eq("email", email);
        return loginMapper.selectOne(wrapper);
    }
}
